package com.vhall.component.controller.account.adm;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import com.vhall.component.controller.account.facade.adm.AdmInfoFacade;
import com.vhall.component.entity.account.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.UrlDisable;
import com.vhall.component.plugin.common.enums.OperateTypeEnum;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.account.AdminInfoService;
import com.vhall.component.service.annotation.OperateLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lsl
 * @since 2021/7/30
 */
@VhallComponent
@UrlDisable
@TokenAuth("sys:admin")
@Tag(name = "管理员列表-管理端")
public class AdmInfoController implements AdmInfoFacade {

    @Autowired
    private AdminInfoService adminInfoService;

    /**
     * 获取管理员信息详情
     *
     * @param reqVo
     * @return
     */
    @Override
    @TokenAuth("sys:admin:info")
    public AdminGetInfoRspVo get(AdminGetInfoReqVo reqVo) {
        return adminInfoService.get(reqVo);
    }

    /**
     * 获取管理员列表
     *
     * @param reqVO
     * @return
     */
    @Override
    @TokenAuth("sys:admin:list")
    public AdminListRspVO list(AdminListReqVO reqVO) {
        return adminInfoService.list(reqVO);
    }

    /**
     * 修改管理员的密码
     *
     * @param reqVO 参数
     * @return
     */
    @Override
    @TokenAuth("sys:admin:pwd")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "修改管理员的密码")
    public Boolean editPassword(AdmEditPasswordReqVO reqVO) {
        return adminInfoService.editPassword(reqVO);
    }

    /**
     * 删除管理员信息
     *
     * @param reqVO
     * @return
     */
    @Override
    @TokenAuth("sys:admin:del")
    @OperateLog(type = OperateTypeEnum.DELETE, name = "删除管理员信息")
    public List<Integer> delete(AdmDeleteReqVO reqVO) {
        return adminInfoService.delete(reqVO);
    }

    /**
     * 修改管理员的账户状态
     *
     * @param reqVO
     * @return
     */
    @Override
    @TokenAuth("sys:admin:status")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "修改管理员的账户状态")
    public Boolean editStatus(AdmEditStatusReqVO reqVO) {
        return adminInfoService.editStatus(reqVO);
    }

    /**
     * 编辑管理员信息
     *
     * @param vo
     * @return
     */
    @Override
    @TokenAuth("sys:admin:edit")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "编辑管理员信息")
    public Boolean edit(AdmEditReqVO vo) {
        if (StringUtils.isNotEmpty(vo.getMobile()) && !PhoneUtil.isMobile(vo.getMobile())) {
            throw new BusinessException(BizErrorCode.TYPE_PHONE);
        }
        vo.setNickName(RandomUtil.randomString(10));
        return adminInfoService.edit(vo);
    }

    /**
     * 创建管理员
     *
     * @param vo
     * @return
     */
    @Override
    @TokenAuth("sys:admin:add")
    @OperateLog(type = OperateTypeEnum.CREATE, name = "创建管理员")
    public AdminAddRspVO add(AdminAddReqVO vo) {
        if (StringUtils.isNotEmpty(vo.getMobile()) && !PhoneUtil.isMobile(vo.getMobile())) {
            throw new BusinessException(BizErrorCode.TYPE_PHONE);
        }
        return adminInfoService.add(vo);
    }
}
